package com.test.exercise.impl;

import com.test.exercise.Sequence;

/**
 * description: SequenceB 实现 Sequence 接口,且使用 ThreadLocal 作为存放每个线程变量的容器
 * @version v1.0
 * @author w
 * @date 2018年12月1日 16:14:12
 **/
public class SequenceB implements Sequence  {
	private static final ThreadLocal<Integer> numContainer = new ThreadLocal<Integer>(){
		@Override
		protected Integer initialValue() {
			return 0;
		};
	};

	@Override
	public int getNum() {
		Integer num = numContainer.get();
		num = num +1 ;
		numContainer.set(num);
		return num;
	}
	
	public static void main(String[] args) {
		// 创建sequence 对象
		Sequence sequence = new SequenceB();
		// 创建3个线程对象
		Thread s1 = new ClientThread(sequence);
		Thread s2 = new ClientThread(sequence);
		Thread s3 = new ClientThread(sequence);
		
		// 启动3个线程
		s1.start();
		s2.start();
		s3.start();
	}
}
